using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Portal.API;
using System.Web.UI;

namespace Portal
{
	/// <summary>
	///	Renders the Headers Tab Menu. 
	/// </summary>
	public partial class AdminMenu : System.Web.UI.UserControl
	{
		private string tabRef = "";
		private string portalId = "";

		private DisplayTabItem BuildDisplayTabItem(PortalDefinition.Tab t)
		{
			if (UserManagement.HasViewRights(Page.User, t.roles))
			{
				return new DisplayTabItem(t, false);
			}
			return null;
		}

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (Request.Params["tabRef"] != null)
			{
				tabRef = Request.Params["tabRef"].ToString();
				portalId = Helper.GetPortalConfig(tabRef);
			}
			menuContainer.Visible = Page.User.IsInRole(Portal.API.AppConfig.AdminRole);

			// Load Protal Definition and the current Tab
			PortalDefinition pd = PortalDefinition.Load(!portalId.Equals("") ? portalId : AppConfig.PORTAL_ADMIN);
			PortalDefinition.Tab currentTab = pd.GetTab(Request["tabRef"]);

			// For Each Tab...
			ArrayList tabList = new ArrayList();
			ArrayList subTabList = new ArrayList();
			foreach (PortalDefinition.Tab t in pd.tabs)
			{
				DisplayTabItem dt = BuildDisplayTabItem(t);
				if (dt != null)
				{
					// Set current Tab Property
					if (currentTab == null)
					{
						if (tabList.Count == 1)
						{
							// First tab -> default
							dt.CurrentTab = true;
						}
					}
					else
					{
						dt.CurrentTab = currentTab.RootTab == t;
					}
					tabList.Add(dt);

					if (dt.CurrentTab && AppConfig.TabMenuShowSubTabs)
					{
						foreach (PortalDefinition.Tab st in t.tabs)
						{
							DisplayTabItem sdt = BuildDisplayTabItem(st);
							if (sdt != null)
							{
								subTabList.Add(sdt);
							}
						}
					}
				}
			}

			mainMenu.DataSource = tabList;
			mainMenu.DataBind();

			if (subTabList.Count > 0)
			{
				subMenu.Visible = true;
				rptSubItem.DataSource = subTabList;
				rptSubItem.DataBind();
			}
			else
			{
				subMenu.Visible = false;
			}
		}

		protected void OnAddTab(object sender, EventArgs args)
		{
			PortalDefinition pd = PortalDefinition.Load(portalId);
			PortalDefinition.Tab t = PortalDefinition.Tab.Create(portalId);

			pd.GetTab(tabRef).tabs.Add(t);

			pd.Save(portalId);

			Response.Redirect(Helper.GetEditTabLink(t.reference));
		}

		protected void OnEditTab(object sender, EventArgs args)
		{
			Response.Redirect(Helper.GetEditTabLink());
		}

		protected void OnDeleteTab(object sender, EventArgs args)
		{
			PortalDefinition.Tab t = PortalDefinition.CurrentAdminTab;
			PortalDefinition.DeleteTab(t.reference, Helper.GetPortalConfig(tabRef));

			if (t.parent == null)
			{
				Response.Redirect(Helper.GetTabLink(""));
			}
			else
			{
				Response.Redirect(Helper.GetTabLink(t.parent.reference));
			}
		}

		protected void OnAddLeftModule(object sender, EventArgs args)
		{
			PortalDefinition pd = PortalDefinition.Load(portalId);
			PortalDefinition.Tab t = pd.GetTab(Request["tabRef"]);
			PortalDefinition.Module m = PortalDefinition.Module.Create();
			t.left.Add(m);

			pd.Save(portalId);

			Response.Redirect(Helper.GetEditModuleLink(m.reference));
		}

		protected void OnAddMiddleModule(object sender, EventArgs args)
		{
			PortalDefinition pd = PortalDefinition.Load(portalId);
			PortalDefinition.Tab t = pd.GetTab(Request["tabRef"]);

			PortalDefinition.Module m = PortalDefinition.Module.Create();
			t.middle.Add(m);

			pd.Save(portalId);

			Response.Redirect(Helper.GetEditModuleLink(m.reference));
		}

		protected void OnAddRightModule(object sender, EventArgs args)
		{
			PortalDefinition pd = PortalDefinition.Load(portalId);
			PortalDefinition.Tab t = pd.GetTab(Request["tabRef"]);

			PortalDefinition.Module m = PortalDefinition.Module.Create();
			t.right.Add(m);

			pd.Save(portalId);

			Response.Redirect(Helper.GetEditModuleLink(m.reference));
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}

		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}
		#endregion

		protected void tabMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				HyperLink tabItem = (HyperLink)e.Item.FindControl("tabItem");
				tabItem.CssClass = ((bool)DataBinder.Eval(e.Item.DataItem, "CurrentTab")) ? "current" : "";
				tabItem.NavigateUrl = (string)DataBinder.Eval(e.Item.DataItem, "URL");
				tabItem.Text = "<span>" + ((string)DataBinder.Eval(e.Item.DataItem, "Text")).Replace(" ", "&nbsp;") + "</span>";
			}
		}

		protected void rptSubItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				HyperLink tabItem = (HyperLink)e.Item.FindControl("tabItem");
				tabItem.CssClass = ((bool)DataBinder.Eval(e.Item.DataItem, "CurrentTab")) ? "TabMenu_CurrentLink" : "TabMenu_Link";
				tabItem.NavigateUrl = (string)DataBinder.Eval(e.Item.DataItem, "URL");
				tabItem.ImageUrl = ((bool)DataBinder.Eval(e.Item.DataItem, "CurrentTab")) ? (string)DataBinder.Eval(e.Item.DataItem, "ImgPathActive") : (string)DataBinder.Eval(e.Item.DataItem, "ImgPathInactive");
				tabItem.Text = ((string)DataBinder.Eval(e.Item.DataItem, "Text")).Replace(" ", "&nbsp;");
			}
		}
	}
}
